<?php
namespace Home\Controller;

use Common\Controller\HomeController;
use Common\Model\TakeModel;
use Think\Page;

class TakeController extends HomeController
{
    /**
     * 取件列表
     * @author Mr.Alien
     * @param $p int 页数
     * @return string
     */
    public function index($p = 1)
    {
        I('get.no') && ($this->where['no'] = array('like','%'.I('get.no').'%'));
        I('get.phone') && ($this->where["right(phone,5)"] = array('like','%'.I('get.phone').'%'));
        I('get.name') && ($this->where['name'] = array('like','%'.I('get.name').'%'));
        I('get.status') && ($this->where['status'] = I('get.status'));
        $this->order = 'status asc';
        $take_time = I('get.take_time');
        $take_time && ($this->order = "take_time $take_time");       
        $this->where['user_id'] = session('id');
        $this->assign('send_template',M('MessContent')->where(array('user_id'=>session('id')))->select());
        parent::index($p);
    }

    /**
     * 修改取件信息
     * @author Mr.Alien
     * @param $id int  记录id
     * @return string
     */
    public function edit($id)
    {
    	 $info = D(CONTROLLER_NAME)->relation(true)->find($id);
        $container = M('Container')
        ->where(array('user_id'=>session('id'),'status'=>2))
        ->where("capacity-used>0 or id = {$info['container_id']} ")
        ->select();
        $this->assign('container',$container);
        $this->assign('info',$info);
        $this->display();
    }

    /**
     * 添加取件信息
     * @author Mr.Alien
     * @return string
     */
    public function add()
    {
        $container = M('Container')
            ->where(array(
            	'user_id'=>session('id'),
            	'status'=>2
            	))->where("capacity-used>0")
            ->select();
        $this->assign('container',$container);
        parent::add();
    }

    /**
     * 保存取件信息
     * @author Mr.Alien
     * @return string
     */
    public function save()
    {
        $_POST['user_id'] = session('id');
        $data = I('post.');
        if($data['id']){
            $has = M('Take')->where(array('no'=>$data['no'],'id'=>array('neq',$data['id'])))->find();
            $has && $this->error('快递号重复');
            $take = M("Take")->where(array('id'=>(int)$data['id']))->find();
            if($take['container_id'] != $data['container_id']){
                M("Container")->where(array('id'=>$data['container_id']))->setInc('used');
                M("Container")->where(array('id'=>$take['container_id']))->setDec("used");
            }
            $res = M(CONTROLLER_NAME)->save($data);
        }else{
            $data['roleid'] = session('roleid');
            $customer = M('Customer')->where(array('phone'=>$data['phone']))->find();
            if($customer){
                $data['card'] = $customer['card'];
                $data['card_image'] = $customer['card_image'];
            }
            $has = M('Take')->where(array('no'=>$data['no']))->find();
            $has && $this->error('快递号重复');
            $res = M(CONTROLLER_NAME)->add($data);
            session('container_id',$data['container_id']);
            if($res !== false){//使用货柜,对货柜使用量加1
                M("Container")->where(array('id'=>$data['container_id']))->setInc('used');
            };
        }
        if($res === false){
            $this->error('保存失败');
        }else{
            $this->success('保存成功');
        }
    }

    /**
     * 客户取件
     * @author Mr.Alien
     * @return string
     */
    public function take($id)
    {
        $info = M('Take')->where(array('id'=>(int)$id))->find();
        $res = M('Take')->where(array('id'=>(int)$id,'user_id'=>session('id')))
            ->save(array('status'=>TakeModel::TAKE_SUCCESS,'take_time'=>date('Y-m-d H:i:s')));
        if($res === false){
            $this->error('保存失败');
        }else{
            M("Container")->where(array('id'=>$info['container_id']))->setDec('used');
			$url = C('DOMAIN').'/Api/ApiList/updateSet?id='.$info['id']."&since=".$info['since'];
			$res = file_get_contents($url);		
            $this->error('保存成功');
        }
    }

    /**
     * 取件复核
     * @Mr.Alien
     * @param $p int 頁數
     * @return string
     */
    public function review($p=1)
    {
        I('get.no') && ($this->where['no'] = array('like','%'.I('get.no').'%'));
        I('get.phone') && ($this->where["right(phone,5)"] = array('like','%'.I('get.phone').'%'));
        I('get.name') && ($this->where['name'] = array('like','%'.I('get.name').'%'));
        $this->where['user_id'] = session('id');
        $this->where['status'] = TakeModel::TAKE_WAIT;
        $list = D(CONTROLLER_NAME)->relation(true)->where($this->where)->page($p,$this->listRows)->order($this->order)->select();
        $page = new Page(D(CONTROLLER_NAME)->where($this->where)->count(),$this->listRows);
        $this->assign('list',$list);
        $this->assign('page',$page->show());
        $this->display();
    }

    /**
     * 待发送列表
     * @author Mr.Alien
     */
    public function sendList()
    {
        $this->assign('list',M("Take")->where(array('user_id'=>session('id'),'mess_status'=>TakeModel::TAKE_WAIT))->select());
        $this->assign('template',M('MessContent')->where(array('user_id'=>session('id')))->select());//模板
        $this->display();
    }

    /**
     * 待发送语音列表
     * @author Mr.Alien
     */
    public function voiceList()
    {
        $this->assign('list',M("Take")->where(array('user_id'=>session('id'),'voice_status'=>TakeModel::TAKE_WAIT))->select());
        $this->display();
    }

    /**
     * 删除快件
     * @author Mr.Alien
     * @return string
     * @param $id int 记录id
     */
    public function del($id)
    {
        $info = M('Take')->where(array('id'=>$id,'user_id'=>session('id')))->find();
        !$info && $this->error('未获取到数据');
        $res = M(CONTROLLER_NAME)->delete((int)$id);
        if($res === false){
            $this->error('删除失败');
        }else{
            if($info['status'] == 1){
              $a = M('Container')->where(array('id'=>$info['container_id']))->setDec('used');
            }
            $this->success('删除成功');
        }
    }

}